package com.ami.kvm.jviewer.kvmpkts;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.gui.JViewerApp;

public class KeepAlive extends Thread {
	private final int SLEEP_TIME = 2000;
	private long sentIdleTime;
	private long recvIdleTime;

	public void run() {
		try {
			for (;;) {
				Thread.sleep(SLEEP_TIME);
				if (JViewerApp.getInstance().getSessionLive()) {
					this.sentIdleTime = (JViewerApp.getInstance().getCurrentTime()
							- JViewerApp.getInstance().getLastPckSent());

					this.recvIdleTime = (JViewerApp.getInstance().getCurrentTime()
							- JViewerApp.getInstance().getLastPcktRecvd());

					if ((this.sentIdleTime > 2000L) || (this.recvIdleTime > 2000L)) {
						JViewerApp.getInstance().onSendKeepAliveRequest();
						Debug.out.println("\nSent KeepAlive Packet - " + this.sentIdleTime);
					}
				}
			}
		} catch (InterruptedException localInterruptedException) {
			localInterruptedException.printStackTrace();
		}
	}
}
